<?php
//Needed model file
header("Content-type: text/html; charset=gbk");
include_once $_SERVER['DOCUMENT_ROOT']."/model/Undertake.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Task.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Template.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Location.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Stage.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Action.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Input.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Output.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/PictureOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/TextOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/EnumOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/NumericalOutput.php";
//Needed dao file
include_once $_SERVER['DOCUMENT_ROOT']."/dao/UndertakeDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TaskDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TemplateDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/LocationDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/ActionDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/InputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/PictureOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TextOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/EnumOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/NumericalOutputDAO.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/31
 * Time: 14:36
 */
const PICTURE_OUTPUT = 0, TEXT_OUTPUT = 1, NUMERICAL_OUTPUT = 2, ENUM_OUTPUT = 3;
//接收post数据，并强制转换为关联数组
$data = $_POST['data'];
$para = json_decode($data,true);
$result = getAcceptedTask($para);
echo urldecode(json_encode($result));

//获得已接任务列表
function getAcceptedTask($para){
    //输入参数
    $userId = $para["userid"];
    // 数据访问对象
    $undertakeDAO = new UndertakeDAO();
    $taskDAO = new TaskDAO();
    $templateDAO = new TemplateDAO();
    $locationDAO = new LocationDAO();
    //根据undertake中的stageId返回信息
    $stages = $undertakeDAO->queryStageListByUserAndStatus($userId,Stage::$STAGE_ONGOING);

    $responses = array();
    foreach($stages as $stage){
        $response = new ResponseBO();
        /**
         * 获得返回值所需要的信息
         */
        $task = $taskDAO->query($stage->getTaskId());
        $template = $templateDAO->query($task->getTemplateId());
        // Get the info about start location and the input
        $src = $locationDAO->queryByStageAndType($stage->getId(), Location::$TYPE_SRC);
        $dest = $locationDAO->queryByStageAndType($stage->getId(), Location::$TYPE_DEST);
        $srcLocInputOutput = null;
        if($src != null)
            $srcLocInputOutput = getInputOutputByLocation($src);
        $destLocInputOutput = null;
        if($dest != null)
            $destLocInputOutput = getInputOutputByLocation($dest);
        $locations = array();
        array_push($locations,$srcLocInputOutput);
        array_push($locations,$destLocInputOutput);
        //查询并返回contract
        $undertake = $undertakeDAO->queryByUserAndStage($userId, $stage->getId());
        $contract = "0000-00-00 00:00:00";
        if($undertake != null)
            $contract = $undertake->getContractTime();


        //构造返回对象信息
        $response->setTaskId($task->getId());
        $response->setTaskTitle($task->getTitle());
        $response->setTaskDesc($task->getDescription());
        $response->setTaskProgress($task->getCurrentStage()."/".$template->getTotalStageNum());
        $response->setCurrentStage($task->getCurrentStage());
        $response->setBonusReward($task->getBonusReward());
        $response->setTaskDeadline($task->getDeadline());
        $response->setStageId($stage->getId());
        $response->setStageName($stage->getName());
        $response->setStageDesc($stage->getDescription());
        $response->setReward($stage->getReward());
        $response->setDdl($stage->getDeadline());
        $response->setContract($contract);
        $response->setLocations($locations);

        array_push($responses,$response);
    }
    return $responses;
}

//根据location获得相应的输入输出
function getInputOutputByLocation($location){
    $locationBO = new LocationBO();
    // DAO of the action, input and four types of output
    $actionDAO = new ActionDAO();
    $inputDAO = new InputDAO();
    $pictureoutputDAO = new PictureOutputDAO();
    $textoutputDAO = new TextOutputDAO();
    $enumoutputDAO = new EnumOutputDAO();
    $numberoutputDAO = new NumericalOutputDAO();

    // Get the input and output list
    $action = $actionDAO->queryActionListByLocation($location->getId())[0];
    $originalInputs = $inputDAO->queryInputListByAction($action->getId());
    //output只需要描述即可
    $pictureoutputs = $pictureoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
    $textoutputs = $textoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
    $enumoutputs = $enumoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
    $numberoutputs = $numberoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
    //根据数据库查询获得的input和output构建inputBO和outputBO
    $inputs = array();
    foreach($originalInputs as $originalInput){
        $input = new InputBO();
        $input->setId($originalInput->getId());
        $input->setActionId($originalInput->getActionId());
        $input->setType($originalInput->getType());
        $input->setDesc($originalInput->getDesc());
        $input->setValue($originalInput->getValue());
        array_push($inputs,$input);
    }
    //四种类型output
    $outputs = array();
    foreach ($pictureoutputs as $pictureoutput){
        $output = new OutputBO();
        $output->setId($pictureoutput->getId());
        $output->setActionId($pictureoutput->getActionId());
        $output->setIsActive($pictureoutput->getisActive());
        $output->setType(PICTURE_OUTPUT);
        $output->setDesc($pictureoutput->getDesc());
        $output->setValue($pictureoutput->getValue());
        array_push($outputs,$output);
    }
    foreach ($textoutputs as $textoutput){
        $output = new OutputBO();
        $output->setId($textoutput->getId());
        $output->setActionId($textoutput->getActionId());
        $output->setType(TEXT_OUTPUT);
        $output->setDesc($textoutput->getDesc());
        $output->setValue($textoutput->getValue());
        array_push($outputs,$output);
    }
    foreach ($enumoutputs as $enumoutput){
        $output = new OutputBO();
        $output->setId($enumoutput->getId());
        $output->setActionId($enumoutput->getActionId());
        $output->setType(ENUM_OUTPUT);
        $output->setDesc($enumoutput->getDesc());
        $output->setValue($enumoutput->getValue());
        $output->setEntries($enumoutput->getEntries());
        $output->setAggregaMethod($enumoutput->getAggregationMethod());
        array_push($outputs,$output);
    }
    foreach($numberoutputs as $numberoutput){
        $output = new OutputBO();
        $output->setId($numberoutput->getId());
        $output->setActionId($numberoutput->getActionId());
        $output->setType(NUMERICAL_OUTPUT);
        $output->setDesc($numberoutput->getDesc());
        $output->setValue($numberoutput->getValue());
        $output->setIntervalValue($numberoutput->getInterval());
        $output->setUpBound($numberoutput->getUpperBound());
        $output->setLowBound($numberoutput->getLowerBound());
        $output->setAggregaMethod($numberoutput->getAggregationMethod());
        array_push($outputs,$output);
    }

    //Set the information of locationBO
    $locationBO->setAddress($location->getAddress());
    $locationBO->setLongitude($location->getLongitude());
    $locationBO->setLatitude($location->getLatitude());
    $locationBO->setType($location->getType());
    $locationBO->setInputs($inputs);
    $locationBO->setOutputs($outputs);
    return $locationBO;
}

class ResponseBO{
    public $taskId;
    public $taskTitle;
    public $taskDesc;
    public $taskProgress;
    public $currentStage;
    public $bonusReward;
    public $taskDeadline;
    public $stageId;
    public $stageName;
    public $stageDesc;
    public $reward;
    public $ddl;
    public $contract;
    public $locations;

    /**
     * @return mixed
     */
    public function getTaskId()
    {
        return $this->taskId;
    }

    /**
     * @param mixed $taskId
     */
    public function setTaskId($taskId)
    {
        $this->taskId = $taskId;
    }

    /**
     * @return mixed
     */
    public function getTaskTitle()
    {
        return $this->taskTitle;
    }

    /**
     * @param mixed $taskTitle
     */
    public function setTaskTitle($taskTitle)
    {
        $this->taskTitle = urlencode($taskTitle);
    }

    /**
     * @return mixed
     */
    public function getTaskDesc()
    {
        return $this->taskDesc;
    }

    /**
     * @param mixed $taskDesc
     */
    public function setTaskDesc($taskDesc)
    {
        $this->taskDesc = urlencode($taskDesc);
    }

    /**
     * @return mixed
     */
    public function getTaskProgress()
    {
        return $this->taskProgress;
    }

    /**
     * @param mixed $taskProgress
     */
    public function setTaskProgress($taskProgress)
    {
        $this->taskProgress = $taskProgress;
    }

    /**
     * @return mixed
     */
    public function getCurrentStage()
    {
        return $this->currentStage;
    }

    /**
     * @param mixed $currentStage
     */
    public function setCurrentStage($currentStage)
    {
        $this->currentStage = $currentStage;
    }

    /**
     * @return mixed
     */
    public function getBonusReward()
    {
        return $this->bonusReward;
    }

    /**
     * @param mixed $bonusReward
     */
    public function setBonusReward($bonusReward)
    {
        $this->bonusReward = $bonusReward;
    }

    /**
     * @return mixed
     */
    public function getTaskDeadline()
    {
        return $this->taskDeadline;
    }

    /**
     * @param mixed $taskDeadline
     */
    public function setTaskDeadline($taskDeadline)
    {
        $this->taskDeadline = $taskDeadline;
    }

    /**
     * @return mixed
     */
    public function getStageId()
    {
        return $this->stageId;
    }

    /**
     * @param mixed $stageId
     */
    public function setStageId($stageId)
    {
        $this->stageId = $stageId;
    }

    /**
     * @return mixed
     */
    public function getStageName()
    {
        return $this->stageName;
    }

    /**
     * @param mixed $stageName
     */
    public function setStageName($stageName)
    {
        $this->stageName = urlencode($stageName);
    }

    /**
     * @return mixed
     */
    public function getStageDesc()
    {
        return $this->stageDesc;
    }

    /**
     * @param mixed $stageDesc
     */
    public function setStageDesc($stageDesc)
    {
        $this->stageDesc = urlencode($stageDesc);
    }

    /**
     * @return mixed
     */
    public function getReward()
    {
        return $this->reward;
    }

    /**
     * @param mixed $reward
     */
    public function setReward($reward)
    {
        $this->reward = $reward;
    }

    /**
     * @return mixed
     */
    public function getDdl()
    {
        return $this->ddl;
    }

    /**
     * @param mixed $ddl
     */
    public function setDdl($ddl)
    {
        $this->ddl = $ddl;
    }

    /**
     * @return mixed
     */
    public function getContract()
    {
        return $this->contract;
    }

    /**
     * @param mixed $contract
     */
    public function setContract($contract)
    {
        $this->contract = $contract;
    }

    /**
     * @return mixed
     */
    public function getLocations()
    {
        return $this->locations;
    }

    /**
     * @param mixed $locations
     */
    public function setLocations($locations)
    {
        $this->locations = $locations;
    }

}

class LocationBO{
    public $address;
    public $longitude;
    public $latitude;
    public $type;
    public $inputs;
    public $outputs;

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address)
    {
        $this->address = urlencode($address);
    }

    /**
     * @return mixed
     */
    public function getLongitude()
    {
        return $this->longitude;
    }

    /**
     * @param mixed $longitude
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;
    }

    /**
     * @return mixed
     */
    public function getLatitude()
    {
        return $this->latitude;
    }

    /**
     * @param mixed $latitude
     */
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getInputs()
    {
        return $this->inputs;
    }

    /**
     * @param mixed $inputs
     */
    public function setInputs($inputs)
    {
        $this->inputs = $inputs;
    }

    /**
     * @return mixed
     */
    public function getOutputs()
    {
        return $this->outputs;
    }

    /**
     * @param mixed $outputs
     */
    public function setOutputs($outputs)
    {
        $this->outputs = $outputs;
    }

}

class InputBO{
    public $id;
    public $actionId;
    public $type;
    public $desc;
    public $value;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getActionId()
    {
        return $this->actionId;
    }

    /**
     * @param mixed $actionId
     */
    public function setActionId($actionId)
    {
        $this->actionId = $actionId;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = urlencode($desc);
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param mixed $value
     */
    public function setValue($value)
    {
        $this->value = urlencode($value);
    }

}

class OutputBO{
    public $id;
    public $actionId;
    public $type;
    public $desc;
    public $value;
    public $isActive;
    public $intervalValue;
    public $upBound;
    public $lowBound;
    public $entries;
    public $aggregaMethod;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getActionId()
    {
        return $this->actionId;
    }

    /**
     * @param mixed $actionId
     */
    public function setActionId($actionId)
    {
        $this->actionId = $actionId;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = urlencode($desc);
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param mixed $value
     */
    public function setValue($value)
    {
        $this->value = urlencode($value);
    }

    /**
     * @return mixed
     */
    public function getisActive()
    {
        return $this->isActive;
    }

    /**
     * @param mixed $isActive
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    /**
     * @return mixed
     */
    public function getIntervalValue()
    {
        return $this->intervalValue;
    }

    /**
     * @param mixed $intervalValue
     */
    public function setIntervalValue($intervalValue)
    {
        $this->intervalValue = $intervalValue;
    }

    /**
     * @return mixed
     */
    public function getUpBound()
    {
        return $this->upBound;
    }

    /**
     * @param mixed $upBound
     */
    public function setUpBound($upBound)
    {
        $this->upBound = $upBound;
    }

    /**
     * @return mixed
     */
    public function getLowBound()
    {
        return $this->lowBound;
    }

    /**
     * @param mixed $lowBound
     */
    public function setLowBound($lowBound)
    {
        $this->lowBound = $lowBound;
    }

    /**
     * @return mixed
     */
    public function getEntries()
    {
        return $this->entries;
    }

    /**
     * @param mixed $entries
     */
    public function setEntries($entries)
    {
        $this->entries = urlencode($entries);
    }

    /**
     * @return mixed
     */
    public function getAggregaMethod()
    {
        return $this->aggregaMethod;
    }

    /**
     * @param mixed $aggregaMethod
     */
    public function setAggregaMethod($aggregaMethod)
    {
        $this->aggregaMethod = $aggregaMethod;
    }

}
?>
